from flask import current_app, jsonify
from flask import g
from flask import make_response
from flask import render_template

from info.models import House, Area
from info.modules.index import index_blu
from utils.common import user_login


@index_blu.route("/", methods=["post", "get"])
@user_login
def index():
    """首页"""
    user = g.user
    # 渲染图片

    data = {
        "user_info": user.to_dict() if user else None
    }

    return render_template("users/index.html", data=data)


@index_blu.route("/api/v1.0/areas")
def areas():

    areas_list = Area.query.filter().all()
    area_dict_li = [area.to_dict() for area in areas_list]

    data = {
        "areas": area_dict_li
    }
    print(area_dict_li)
    # resp = make_response(areas)
    return jsonify(errno="0", data=data)


@index_blu.route("/favicon.ico")
def favicon():
    """ 返回图片"""
    return current_app.send_static_file("favicon.ico")


@index_blu.route('/login.html')
def login():
    """点击登录按钮跳转到登录界面"""
    return render_template("users/login.html")


@index_blu.route("/index.html")
def show_user_name():

    data = {}
    """在登录界面点击登录跳转到首页"""
    return render_template("users/index.html", errno="0", data=data)


@index_blu.route("/api/v1.0/session")
def show_usr_name():
    """登录之后在首页显示用户名"""
    return jsonify(errno="0", errmsg="登录成功")

